class Heap<T> {
  data: T[] = [];
  private length: number = 0;

  //根据索引交换数组里的数据
  private swap(i: number, j: number) {
    const temp = this.data[i];
    this.data[i] = this.data[j];
    this.data[j] = temp;
  }

  //方法
  insert(value: T) {
    //新加入的插入数组最后面
    this.data.push(value);
    this.length++;
    this.percolateUp();
  }

  //上滤操作
  protected percolateUp() {
    let index = this.length - 1;
    while (index > 0) {
      let parentIndex = Math.floor((index - 1) / 2);
      if (this.data[index] <= this.data[parentIndex]) {
        break;
      }
      this.swap(index, parentIndex);
      index = parentIndex;
    }
  }

  extract(): T | undefined {
    return undefined;
  }

  peek(): T | undefined {
    return this.data[0];
  }

  size(): number {
    return this.data.length;
  }

  isEmpty() {
    return this.length === 0;
  }

  buildHeap(arr: T[]) {}
}

const heap = new Heap<number>();

// const arr = [19, 100, 36, 17, 3, 25, 1, 2, 7];
const arr = [21, 19, 78, 95];

arr.forEach(v => {
  heap.insert(v);
});

console.log(heap.data);

export {};
